package com.ssms.config;

import com.ssms.intercept.PermissionIntercept;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Value("${application.profile}")
    private String profile;

    // url与template映射
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addRedirectViewController("/ssms","index");
//        registry.addRedirectViewController("/ssms/index","index");
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/file/**").addResourceLocations("file:" + profile);
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(permissionIntercept())
                .addPathPatterns("/**")
                // 放行静态资源（springboot中static为默认的静态资源路径之一，所以不需要添加static目录名）
                .excludePathPatterns("/css/**","/img/**","/js/**","/layui/**")
                .excludePathPatterns("/")
                .excludePathPatterns("/file/**")
                .excludePathPatterns("/sys/login");
    }

    @Bean
    public PermissionIntercept permissionIntercept(){
        return new PermissionIntercept();
    }
}
